package com.mms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.mms.dao.PersonalInfoDao;
import com.mms.entity.Department;
import com.mms.entity.Staff;
import com.opensymphony.xwork2.Action;

public class PersonalAction implements Action{

    
    private String userName;
    private String phone;
    private int sex;
    private int departmentSe;
    private String newPwdInput;
    
    public String getPersonalInfo() throws UnsupportedEncodingException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        PersonalInfoDao pInfoDao=new PersonalInfoDao();
        Staff staff=pInfoDao.getPersonalInfo(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        request.setAttribute("staff", staff);
        request.getSession().setAttribute("isModifyPersonalSuccessful", false);
        return SUCCESS;
    }
    
    public String getStaffPassword() throws IOException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        PersonalInfoDao pInfoDao=new PersonalInfoDao();
        System.out.println(request.getParameter("sID"));
        String pwd=pInfoDao.getStaffPassword(Integer.parseInt(request.getParameter("sID")));
        List<String> strings=new ArrayList<String>();
        strings.add(pwd);
        JSONArray jsonArray = JSONArray.fromObject(strings); 
        System.out.println(jsonArray.toString());
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(jsonArray.toString());
        out.flush();
        out.close();
        return SUCCESS;
    }
    
    public String modifyPersonalInfo() throws UnsupportedEncodingException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        Staff staff=new Staff();
        PersonalInfoDao pInfoDao=new PersonalInfoDao();
        Department department=new Department();
        department.setDeId(departmentSe);
        staff.setDepartment(department);
        staff.setPassword(newPwdInput);
        staff.setPhone(phone);
        staff.setSex(sex);
        staff.setSName(userName);
        staff.setSId(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        if (pInfoDao.modifyPersonalInfo(staff)) {
            request.setAttribute("isModifyPersonalSuccessful", true);
        }
        Staff staffInfo=pInfoDao.getPersonalInfo(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        request.setAttribute("staff", staffInfo);
        return SUCCESS;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
    
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getDepartmentSe() {
        return departmentSe;
    }

    public void setDepartmentSe(int departmentSe) {
        this.departmentSe = departmentSe;
    }

    public String getNewPwdInput() {
        return newPwdInput;
    }

    public void setNewPwdInput(String newPwdInput) {
        this.newPwdInput = newPwdInput;
    }

}
